using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Angel.Daos.Manager;
using Angel.Entities;
using Angel.Web.Models;
using Angel.Daos.IManager;


namespace Angel.Web.Controllers
{
    public class UserController : GeneralController
    {
        private IUserManager userManager;
        private IGroupManager groupManager;
        private IGroup_UserManager group_userManager;
        IEnumerable<User> kq;
        List<FilterUser> listUser;

        public UserController()
            : this(new UserManager(), new GroupManager(), new Group_UserManager())
        {
        }

        public UserController(IUserManager _userManager, IGroupManager _groupManager, IGroup_UserManager _group_userManager)
        {
            userManager = _userManager;
            groupManager = _groupManager;
            group_userManager = _group_userManager;
        }

        public ActionResult Index()
        {
            List<Group> listGroup = groupManager.GetAll().ToList();
            listGroup.Add(new Group { GroupID = 0, GroupName = "Any" });
            ViewBag.Groups = new SelectList(listGroup, "GroupID", "GroupName");
            IList<object> listCountFilters = new List<object> { "All", 5, 10, 15 };
            ViewBag.ListCountFilters = new SelectList(listCountFilters);
            int page = 1;
            int total;
            kq = userManager.Get(page,10, out total).ToList();
            FilterModel viewModel = new FilterModel
            {
                list = new List<FilterUser>()
            };
            foreach (var item in kq)
            {
                FilterUser user = new FilterUser();
                user.UserID = item.UserID;
                user.UserName = item.UserName;
                user.FullName = item.FullName;
                user.Email = item.Email;
                user.Groups = group_userManager.GetGroupByUserID(item.UserID).ToList();
                viewModel.list.Add(user);
            }
            ViewBag.numberUserDisplay = viewModel.list.Count();
            ViewBag.page = page;
            ViewBag.total = total;
            ViewBag.NumberUsers = userManager.GetAll().Count();
            return View(viewModel);
        }

        public ActionResult IndexPage(int page)
        {
            List<Group> listGroup = groupManager.GetAll().ToList();
            listGroup.Add(new Group { GroupID = 0, GroupName = "Any" });
            ViewBag.Groups = new SelectList(listGroup, "GroupID", "GroupName");
            IList<object> listCountFilters = new List<object> { "All", 5, 10, 15 };
            ViewBag.ListCountFilters = new SelectList(listCountFilters);
            int total;
            kq = userManager.Get(page, 10, out total).ToList();
            listUser = new List<FilterUser>();
            foreach (var item in kq)
            {
                FilterUser user = new FilterUser();
                user.UserID = item.UserID;
                user.UserName = item.UserName;
                user.FullName = item.FullName;
                user.Email = item.Email;
                user.Groups = group_userManager.GetGroupByUserID(item.UserID).ToList();
                listUser.Add(user);
            }
            ViewBag.numberUserDisplay = listUser.Count();
            ViewBag.page = page;
            ViewBag.total = total;
            ViewBag.NumberUsers = userManager.GetAll().Count();
            return PartialView("Filtered", listUser);
        }

        public ActionResult Index_Filter(FilterModel filterModel)
        {
            int page = 1;
            int total;
            try
            {
                kq = userManager.Find(filterModel.userNameContain, filterModel.fullNameContain, filterModel.emailContain, filterModel.groupId, page, filterModel.count, out total);
            }
            catch (Exception)
            {
                filterModel.count = 10;
                kq = userManager.Find(filterModel.userNameContain, filterModel.fullNameContain, filterModel.emailContain, filterModel.groupId, page, filterModel.count, out total);
            }
            listUser = new List<FilterUser>();
            foreach (var item in kq)
            {
                FilterUser user = new FilterUser();
                user.UserID = item.UserID;
                user.UserName = item.UserName;
                user.FullName = item.FullName;
                user.Email = item.Email;
                user.Groups = group_userManager.GetGroupByUserID(item.UserID).ToList();
                listUser.Add(user);
            }
            ViewBag.numberUserDisplay = listUser.Count();
            ViewBag.page = page;
            ViewBag.total = total;
            ViewBag.NumberUsers = userManager.GetAll().Count();
            return PartialView("Filtered", listUser);
        }

        public ActionResult Add()
        {
            ViewBag.Users = userManager.GetAll().Count();
            return View();
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Add(AddModel us)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    User user = new User();
                    user.UserName = us.UserName.Trim();
                    if (us.Password == null)
                        user.PassWord = us.UserName;
                    else
                        user.PassWord = us.Password.Trim();
                    user.FullName = us.fullName.Trim();
                    user.Email = us.Email.Trim();
                    userManager.Add(user);
                    ViewBag.ErrUsername = "";
                    ViewBag.ErrEmail = "";
                    return RedirectToAction("Index");
                }
                catch (Exception e)
                {
                    ViewBag.MessageErr = e.Message;
                    return RedirectToAction("Index", "General");
                }
            }
            else
            {
                return View("Add");
            }
        }

        //Get/id
        public ActionResult Edit(int? id)
        {
            try
            {
                if (id == null)
                {
                    return RedirectToAction("NotFound", "General", "Errror id can not be null");
                }
                else
                {
                    var user = userManager.GetById(id.Value);
                    EditUserModel editUserModel = new EditUserModel();
                    editUserModel.Id = user.UserID;
                    editUserModel.FullName = user.FullName;
                    editUserModel.Email = user.Email;
                    return View(editUserModel);
                }
            }
            catch
            {
                return RedirectToAction("NotFound", "General", @"Error definition");
            }
        }
        [HttpPost]
        public ActionResult Edit(EditUserModel editUserModel)
        {
            try
            {
                var user = userManager.GetById(editUserModel.Id);
                UpdateModel(user);
                //Go back to the home
                userManager.Save(user);
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
               // ViewBag.MessageErr = e.Message;

            }
            return View();
        }

        public ActionResult Delete(int? id)
        {
            try
            {
                if (id == null)
                {
                    return RedirectToAction("NotFound", "General", "Error id");
                }
                var user = userManager.GetById(id.Value);
                DeleteUserModel deleteUserModel = new DeleteUserModel();
                deleteUserModel.Id = user.UserID;
                deleteUserModel.username = user.UserName;
                return View(deleteUserModel);
            }
            catch
            {
                return RedirectToAction("NotFound", "General");
            }
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            try
            {
                userManager.Delete(id);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = ex.Message;
            }
            return View();
        }

        public ActionResult GlobalPermission()
        {
            return View();
        }
        public ActionResult ShareFilter()
        {
            return View();
        }
        public ActionResult SharedDashboards()
        {
            return View();
        }
        public ActionResult UserDirectory()
        {
            return View();
        }
        public ActionResult UserPreference()
        {
            return View();
        }
    }
}